#ifndef DBINTERFACE_H
#define DBINTERFACE_H
#include <string>
#include "sqlite3.h"
#include <vector>



using namespace std;

typedef struct Mayorinfo
{
	string strname;
	int iprice;
	int iStore;
	int isellout;
}MayorInfo;

typedef struct Sellinfo
{
	int isellid;
	string name;
	int iprice;
	int isellnum;
}sellinfo;


typedef struct sqlresult
{
	char ** azresult;
	char * errmsg;
	int nrow;
	int ncol;
	int sqlState;
}sqlResult;

typedef struct  BuyInfo
{
	int ibuyID;
	string  strname;
	int iprice;
	int ibuynum;
	string strbak;
}buyInfo;

class CDBInterface
{
public:
    
	CDBInterface()
	{
		pDB=NULL;
		m_strPath="..\\my.db";
		isopen=false;
	}

	int select1();
	int insert1();

	int createTable(string sqlCreatTable);
    
	int open();

	sqlite3* getpdb()
	{
		return pDB;
	}
	sqlite3 ** getppdb()
	{
		return &pDB;
	}

	int insertGoods2DB(string itemName ,string itemprice,string itemActualCost,
		string itemQuantity,string 	itemsoldout	,string itempicPath,string itemserialcode );

	int AddUser(string username,string passwd,string email);

	string getDatabyCondition(string tblname,string colname,string condition);

	void close(void)
	{
		sqlite3_close(pDB);
		isopen=false;
	}

	char ** CDBInterface::GetRecBySerialCode(string tblname,string itemSerialCode,int * nrow ,int * ncol);
	int delOneRec(string tblname,string name,string value);

	bool Isopen(){return isopen;};
	bool isopen;
	static CDBInterface * getDBInterface()
	{
		if(NULL == ptrDBobject){
			 ptrDBobject =new CDBInterface();
			 return ptrDBobject;
		}
		else{
			return ptrDBobject;
		}
	}

	int getSqlReadResult( sqlResult * psqlresult,string sqlstr);

	void creatBuyHistoryTbl();
	void creatSellHistoryTbl();
	void creatCurInfoTbl();
	int getMayorInfo(MayorInfo *pinfo,string name);
	int getSellinfo(vector<struct Sellinfo>  & sellInfoSet,  string name);
//int CDBInterface::getSellinfo(vector<sellinfo> &  sellInfoSet,string name)
	//int getSellinfo(vector<sellinfo> &  sellInfoSet,string name);

	int getBuyinfo(vector<buyInfo> &  buInfoSet,string name);
	int buyGoods( buyInfo   buyinfo);
	int sellGoods( sellinfo sellInfo);

private:
	sqlite3* pDB;
	string m_strPath;
	static CDBInterface * ptrDBobject;
};

static int callback(void *NotUsed, int argc, char **argv, char **azColName);
#endif
